﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Reflection;

namespace Utility
{
    public class MyDataGridView : DataGridView
    {
        public enum eRowMoveMode
        {
            UP,         //向上移动一行
            DOWN,       //向下移动一行
        }


        public MyDataGridView()
        {
            Type dgvType = this.GetType();
            PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(this, true, null);
            this.DoubleBuffered = true;

            this.AllowUserToAddRows = false;
            //this.ReadOnly = true;
            this.AutoGenerateColumns = true;
            this.RowHeadersVisible = false;

            this.BackgroundColor = System.Drawing.SystemColors.Control;
            this.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;



            this.RowsDefaultCellStyle.BackColor = Color.LightSteelBlue;

            ////设定包括Header和所有单元格的列宽自动调整
            //this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            // 设定包括Header和所有单元格的行高自动调整
            this.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        }

        protected override void OnColumnAdded(DataGridViewColumnEventArgs e)
        {
            try
            {
                e.Column.SortMode = DataGridViewColumnSortMode.NotSortable;//列标题右边有预留一个排序小箭头的位置，导致没有完全居中,所以禁用排序
            }
            catch (Exception)
            {

            }
            base.OnColumnAdded(e);
        }

        /// <summary>
        /// 上下移动一行
        /// </summary>
        /// <param name="mode"></param>
        public void dvgRowUpDown(eRowMoveMode mode)
        {
            if(this.SelectedRows == null || this.SelectedRows.Count == 0)
            {
                return;
            }
            else
            {
                if(this.SelectedRows[0].Index >= this.Rows.Count - 1 && mode == eRowMoveMode.DOWN)
                {
                    return;
                }
                else if (this.SelectedRows[0].Index <= 0 && mode == eRowMoveMode.UP)
                {
                    return;
                }
                else
                {
                    //注意：这里是非绑定数据情况的上移行
                    // 选择的行号 
                    int selectRowIndex = GetSelectedRowIndex();
                        //注意：这里是非绑定数据情况的上移行
                        // 选择的行号  
                    int selectedRowIndex = GetSelectedRowIndex();
                    if (mode == eRowMoveMode.UP)
                    {
                        // 拷贝选中的行  
                        DataGridViewRow newRow = this.Rows[selectedRowIndex];
                        // 删除选中的行  
                        this.Rows.Remove(this.Rows[selectedRowIndex]);
                        // 将拷贝的行，插入到选中的上一行位置  
                        this.Rows.Insert(selectedRowIndex - 1, newRow);
                        this.ClearSelection();
                        // 选中最初选中的行 
                        this.Rows[selectedRowIndex - 1].Selected = true;
                    }
                    else if(mode == eRowMoveMode.DOWN && selectedRowIndex < this.Rows.Count - 1)
                    {
                        // 拷贝选中的行  
                        DataGridViewRow newRow = this.Rows[selectedRowIndex];
                        // 删除选中的行  
                        this.Rows.Remove(this.Rows[selectedRowIndex]);
                        // 将拷贝的行，插入到选中的下一行位置  
                        this.Rows.Insert(selectedRowIndex + 1, newRow);
                        this.ClearSelection();
                        // 选中最初选中的行  
                        this.Rows[selectedRowIndex + 1].Selected = true;
                    }
                }
            }
        }

        protected int GetSelectedRowIndex()
        {
            if (this.Rows.Count == 0)
            {
                return 0;
            }
            foreach (DataGridViewRow row in this.Rows)
            {
                if (row.Selected)
                {
                    return row.Index;
                }
            }
            return 0;
        }

    }
}
